//
//  VisitorView.swift
//  glzbowei
//
//  Created by 高伦展 on 2017/11/19.
//  Copyright © 2017年 glz. All rights reserved.
//

import UIKit
import SnapKit

///// 访客视图的协议（1）
//protocol VisitorViewDelegate:NSObjectProtocol {
//    /// 注册
//    func visitorViewDidRegistor()
//    /// 登录
//    func visitorViewDidLogin()
//}

class VisitorView: UIView {
    
//    ///代理 （2）
//    weak var delegate : VisitorViewDelegate?
//
//    //MARK: - 监听方法 （3）
//    @objc private func clickLogin(){
//        delegate?.visitorViewDidLogin()
//    }
//
//    @objc private func clickRegistor(){
//        delegate?.visitorViewDidRegistor()
//    }
    
    //MARK: - 设置视图信息
    func setupInfo(imageName:String?,title:String){
        messageLabel.text = title
        
        guard let img = imageName else {
            startAnim()
            
            return
        }
        
        iconView.image = UIImage(named:img)
        //隐藏小房间
        homeIconView.isHidden = true
        //将遮罩层移动的底部
        sendSubview(toBack: maskIconView)
    }
    
    //开启首页转轮动画
    private func startAnim(){
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = 2.0 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 20
        //用在不断重复的动画上，当动画绑定的图层对应的视图被销毁，动画会自动销毁
        anim.isRemovedOnCompletion = false
        
        //添加到图层
        iconView.layer.add(anim, forKey: nil)
        
    }
    
    //MARK: - 构造函数
    //initWithFrame 时 UIView 的指定构造函数
    //使用纯代码开发使用的
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    //initWithCoder - 使用 SB & XIB 开发加载的函数
    //使用 SB XIB 开始入口
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        setupUI()
    }
    
    // MARK: - 懒加载控件
    private lazy var iconView:UIImageView = UIImageView(imgName: "visitordiscover_feed_image_smallicon")
    
    private lazy var maskIconView:UIImageView = UIImageView(imgName: "visitordiscover_feed_mask_smallicon")
    
    private lazy var homeIconView:UIImageView = UIImageView(imgName: "visitordiscover_feed_image_house")
    
    private lazy var messageLabel:UILabel = UILabel(title: "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜")
 
     lazy var registerButton:UIButton = UIButton(title: "注册", color: UIColor.orange, backImgName: "common_button_white_disable")
  
     lazy var loginButton:UIButton = UIButton(title: "登录", color: UIColor.darkGray, backImgName: "common_button_white_disable")
  
}

extension VisitorView{
    
    ///设置界面
    private func setupUI(){
        //添加控件
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        //translatesAutoresizingMaskIntoConstraints 默认是 true，支持使用 setFrame 的方式设定控件位置
        //false 支持使用自动布局来设置控件位置
        //iconView.translatesAutoresizingMaskIntoConstraints = false
        
        for view in subviews{
            view.translatesAutoresizingMaskIntoConstraints = false
        }
        
        //小圆圈
//        addConstraint(NSLayoutConstraint(item: iconView, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1.0, constant: 0))
//        addConstraint(NSLayoutConstraint(item: iconView, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1.0, constant: -60))
        
        iconView.snp.makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp.centerX)
            make.centerY.equalTo(self.snp.centerY).offset(-60.0)
        }
        
        //遮罩层
        /**
         VFL : 可视化格式语言
         
         H :水平方向
         V :垂直方向
         | :边界
         [] :包装控件
         
         views: 是一个字典[名字:控件名] -VFL 字符串中表示控件的字符串
         metrics: 是一个字典[名字:控件名] -VFL 字符串中表示某一个数值
         */
//        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[mask]-0-|", options: [], metrics: nil, views: ["mask" : maskIconView]))
//        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[mask]-(btnHeight)-[regBtn]", options: [], metrics: ["btnHeight":-36], views: ["mask" : maskIconView,"regBtn":registerButton]))
        maskIconView.snp.makeConstraints { (make) in
            make.top.equalTo(self.snp.top)
            make.left.equalTo(self.snp.left)
            make.right.equalTo(self.snp.right)
            make.bottom.equalTo(registerButton.snp.bottom)
        }
        
        
        //home
//        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: .centerX, relatedBy: .equal, toItem: iconView, attribute: .centerX, multiplier: 1.0, constant: 0))
//        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: .centerY, relatedBy: .equal, toItem: iconView, attribute: .centerY, multiplier: 1.0, constant: 0))
        
        homeIconView.snp.makeConstraints { (make) in
            make.center.equalTo(iconView)
        }
        
        //msg
//        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .centerX, relatedBy: .equal, toItem: iconView, attribute: .centerX, multiplier: 1.0, constant: 0))
//        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .top, relatedBy: .equal, toItem: iconView, attribute: .bottom, multiplier: 1.0, constant: 16))
//        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 234))
//        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 36))
        
        messageLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(iconView.snp.centerX)
            make.top.equalTo(iconView.snp.bottom).offset(16.0)
            make.width.equalTo(234)
            make.height.equalTo(36)
        }
        
        //register
//        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .left, relatedBy: .equal, toItem: messageLabel, attribute: .left, multiplier: 1.0, constant: 0))
//        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .top, relatedBy: .equal, toItem: messageLabel, attribute: .bottom, multiplier: 1.0, constant: 16))
//        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 100))
//        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 36))
        
        registerButton.snp.makeConstraints { (make) in
            make.left.equalTo(messageLabel.snp.left)
            make.top.equalTo(messageLabel.snp.bottom)
            make.width.equalTo(100)
            make.height.equalTo(36)
        }
        
        //login
//        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .right, relatedBy: .equal, toItem: messageLabel, attribute: .right, multiplier: 1.0, constant: 0))
//        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .top, relatedBy: .equal, toItem: messageLabel, attribute: .bottom, multiplier: 1.0, constant: 16))
//        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 100))
//        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 36))
        
        loginButton.snp.makeConstraints { (make) in
//            make.right.equalTo(messageLabel.snp.right)
//            make.top.equalTo(messageLabel.snp.bottom)
//            make.width.equalTo(100)
//            make.height.equalTo(36)
            make.right.equalTo(messageLabel.snp.right)
            make.top.equalTo(registerButton.snp.top)
            make.width.equalTo(registerButton.snp.width)
            make.height.equalTo(registerButton.snp.height)
        }
        
        backgroundColor = UIColor(white: 237.0/255.0, alpha: 1.0)
        
//        // 添加事件 （4）
//        registerButton.addTarget(self, action: #selector(VisitorView.clickRegistor), for: .touchUpInside)
//
//        loginButton.addTarget(self, action: #selector(VisitorView.clickLogin), for: .touchUpInside)
    }
}
